package org.example.iris.service.impl;

import org.example.iris.dto.StudentInfo;
import org.example.iris.mapper.*;
import org.example.iris.entity.*;
import org.example.iris.service.StudentService;
import org.example.iris.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;


@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    StudentMapper studentMapper;
    @Autowired
    private ExperienceMapper experienceMapper;

    @Autowired
    private ApplicationMapper applicationMapper;

    @Override
    public void register(User user, Student student ) {
        userMapper.insertUser(user);
        User user1 = userMapper.findByUsername(user.getUsername());
        student.setUserId(user1.getUserId());

        studentMapper.insertStudent(student);
    }

    @Override
    public Student findByUserId(Integer userId) {
        return studentMapper.findByUserId(userId);
    }

    @Override
    public void updateStuInfo(Student student) {
        studentMapper.update(student);
    }

    @Override
    public StudentInfo getStuInfo(User user, Student student) {

        StudentInfo studentInfo = new StudentInfo();

        studentInfo.setUsername(user.getUsername());
        studentInfo.setPhone(user.getPhone());
        studentInfo.setEmail(user.getEmail());

        studentInfo.setRealName(student.getRealName());
        studentInfo.setGender(student.getGender());
        studentInfo.setWechatNum(student.getWechatNum());
        studentInfo.setBirth(student.getBirth());
        studentInfo.setJobStatusSelected(student.getJobStatusSelected());
        studentInfo.setSkill(student.getSkill());
        studentInfo.setProfilePicturePath(student.getProfilePicture());

        List<DesiredJob> desiredJobs = experienceMapper.findDesiredJobByStuId(student.getStudentId());
        for (DesiredJob desiredJob : desiredJobs) {
            if (desiredJob.getWorkCityString() != null && !desiredJob.getWorkCityString().isEmpty()) {
                List<String> workCity = Arrays.asList(desiredJob.getWorkCityString().split(","));
                desiredJob.setWorkCity(workCity);
            }
            if (desiredJob.getSalaryRangeString() != null && !desiredJob.getSalaryRangeString().isEmpty()) {
                List<String> salaryRange = Arrays.asList(desiredJob.getSalaryRangeString().split(","));
                desiredJob.setSalaryRange(salaryRange);
            }
        }
        List<EducationExperience> educationExperiences = experienceMapper.findEducationExperienceByStuId(student.getStudentId());
        for (EducationExperience educationExperience : educationExperiences) {
            if (educationExperience.getSchoolTimeRangeString() != null && !educationExperience.getSchoolTimeRangeString().isEmpty()) {
                List<String> schoolTimeRange = Arrays.asList(educationExperience.getSchoolTimeRangeString().split(","));
                educationExperience.setSchoolTimeRange(schoolTimeRange);
            }
        }
        List<WorkExperience> workExperiences = experienceMapper.findWorkExperienceByStuId(student.getStudentId());
        for (WorkExperience workExperience : workExperiences) {
            if (workExperience.getJobTimeRangeString() != null && !workExperience.getJobTimeRangeString().isEmpty()) {
                List<String> jobTimeRange = Arrays.asList(workExperience.getJobTimeRangeString().split(","));
                workExperience.setJobTimeRange(jobTimeRange);
            }
        }
        List<ProjectExperience> projectExperiences = experienceMapper.findProjectExperienceByStuId(student.getStudentId());
        for (ProjectExperience projectExperience : projectExperiences) {
            if (projectExperience.getProjectTimeRangeString() != null && !projectExperience.getProjectTimeRangeString().isEmpty()) {
                List<String> projectTimeRange = Arrays.asList(projectExperience.getProjectTimeRangeString().split(","));
                projectExperience.setProjectTimeRange(projectTimeRange);
            }
        }

        studentInfo.setDesiredJobs(desiredJobs);
        studentInfo.setEducationExperiences(educationExperiences);
        studentInfo.setWorkExperiences(workExperiences);
        studentInfo.setProjectExperiences(projectExperiences);

        return studentInfo;
    }

    @Override
    public String updateStuPicture(MultipartFile file, Integer userId) throws Exception {
        String originalFilename = file.getOriginalFilename();
        String filename = null;
        if (originalFilename != null) {
            filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        //file.transferTo(new File("C:\\Users\\86191\\Pictures\\temp\\"+filename));
        String url = AliOssUtil.UploadFile(filename, file.getInputStream());
        studentMapper.updatePicture(url, userId);
        return url;
    }

    @Override
    public void updateStuStatus(Integer userId, String status) {
        studentMapper.updateStatus(userId, status);
    }

    @Override
    public void updateStuSkill(Integer userId, String skills) {
        studentMapper.updateSkill(userId, skills);
    }

    @Override
    public void updateStuDesiredJob(Integer userId, DesiredJob desiredJob) {
        Student student = studentMapper.findByUserId(userId);
        Integer studentId = student.getStudentId();

        if(experienceMapper.findStuByDesiredId(desiredJob.getDesiredJobId()) == null){
            throw new RuntimeException("求职意向id不存在!");
        }

        if(!studentId.equals(experienceMapper.findStuByDesiredId(desiredJob.getDesiredJobId()))){
            throw new RuntimeException("求职意向id在该学生中不存在!");
        }

        experienceMapper.updateDesiredJob(desiredJob);
    }

    @Override
    public void updateStuWorkExperience(Integer userId, WorkExperience workExperience) {
        Student student = studentMapper.findByUserId(userId);
        Integer studentId = student.getStudentId();

        if(experienceMapper.findStuByWorkExperienceId(workExperience.getWorkExperienceId()) == null){
            throw new RuntimeException("工作经历不存在!");
        }

        if(!studentId.equals(experienceMapper.findStuByWorkExperienceId(workExperience.getWorkExperienceId()))){
            throw new RuntimeException("工作经历id在该学生中不存在!");
        }

        experienceMapper.updateWorkExperience(workExperience);
    }

    @Override
    public void updateStuProjectExperience(Integer userId, ProjectExperience projectExperience) {
        Student student = studentMapper.findByUserId(userId);
        Integer studentId = student.getStudentId();

        if(experienceMapper.findStuByProjectExperienceId(projectExperience.getProjectExperienceId()) == null){
            throw new RuntimeException("项目经历不存在!");
        }

        if(!studentId.equals(experienceMapper.findStuByProjectExperienceId(projectExperience.getProjectExperienceId()))){
            throw new RuntimeException("项目经历id在该学生中不存在!");
        }

        experienceMapper.updateProjectExperience(projectExperience);
    }

    @Override
    public void updateStuEducationExperience(Integer userId, EducationExperience educationExperience) {
        Student student = studentMapper.findByUserId(userId);
        Integer studentId = student.getStudentId();

        if(experienceMapper.findStuByEducationExperienceId(educationExperience.getEducationExperienceId()) == null){
            throw new RuntimeException("教育经历不存在!");
        }

        if(!studentId.equals(experienceMapper.findStuByEducationExperienceId(educationExperience.getEducationExperienceId()))){
            throw new RuntimeException("教育经历id在该学生中不存在!");
        }

        experienceMapper.updateEducationExperience(educationExperience);
    }

    @Override
    public void addStuDesiredJob(Integer userId, DesiredJob desiredJob) {
        Student student = studentMapper.findByUserId(userId);
        Integer studentId = student.getStudentId();

        experienceMapper.addDesiredJob(desiredJob);
        Integer desiredJobId = desiredJob.getDesiredJobId();
        experienceMapper.addStuDesired(studentId, desiredJobId);
    }

    @Override
    public void addStuEducationExperience(Integer userId, EducationExperience educationExperience) {
        Student student = studentMapper.findByUserId(userId);
        Integer studentId = student.getStudentId();

        experienceMapper.addEducationExperience(educationExperience);
        Integer educationExperienceId = educationExperience.getEducationExperienceId();
        experienceMapper.addStuEducation(studentId, educationExperienceId);
    }

    @Override
    public void addStuWorkExperience(Integer userId, WorkExperience workExperience) {
        Student student = studentMapper.findByUserId(userId);
        Integer studentId = student.getStudentId();

        experienceMapper.addWorkExperience(workExperience);
        Integer workExperienceId = workExperience.getWorkExperienceId();
        experienceMapper.addStuWork(studentId, workExperienceId);
    }

    @Override
    public void addStuProjectExperience(Integer userId, ProjectExperience projectExperience) {
        Student student = studentMapper.findByUserId(userId);
        Integer studentId = student.getStudentId();

        experienceMapper.addProjectExperience(projectExperience);
        Integer projectExperienceId = projectExperience.getProjectExperienceId();
        experienceMapper.addStuProject(studentId, projectExperienceId);
    }

    @Override
    public void deleteDesiredJob(Integer userId, Integer desiredJobId) {
        Student student = studentMapper.findByUserId(userId);
        Integer studentId = student.getStudentId();

        if(experienceMapper.findStuByDesiredId(desiredJobId) == null){
            throw new RuntimeException("求职意向id不存在!");
        }

        if(!studentId.equals(experienceMapper.findStuByDesiredId(desiredJobId))){
            throw new RuntimeException("求职意向id在该学生中不存在!");
        }

        experienceMapper.deleteStuDesired(desiredJobId);
        experienceMapper.deleteDesiredJob(desiredJobId);
    }

    @Override
    public void deleteEducationExperience(Integer userId, Integer educationExperienceId) {
        Student student = studentMapper.findByUserId(userId);
        Integer studentId = student.getStudentId();

        if(experienceMapper.findStuByEducationExperienceId(educationExperienceId) == null){
            throw new RuntimeException("教育经历id不存在!");
        }

        if(!studentId.equals(experienceMapper.findStuByEducationExperienceId(educationExperienceId))){
            throw new RuntimeException("教育经历id在该学生中不存在!");
        }

        experienceMapper.deleteStuEducation(educationExperienceId);
        experienceMapper.deleteEducationExperience(educationExperienceId);
    }

    @Override
    public void deleteWorkExperience(Integer userId, Integer workExperienceId) {
        Student student = studentMapper.findByUserId(userId);
        Integer studentId = student.getStudentId();

        if(experienceMapper.findStuByWorkExperienceId(workExperienceId) == null){
            throw new RuntimeException("工作经历id不存在!");
        }

        if(!studentId.equals(experienceMapper.findStuByWorkExperienceId(workExperienceId))){
            throw new RuntimeException("工作经历id在该学生中不存在!");
        }

        experienceMapper.deleteStuWork(workExperienceId);
        experienceMapper.deleteWorkExperience(workExperienceId);
    }

    @Override
    public void deleteProjectExperience(Integer userId, Integer projectExperienceId) {
        Student student = studentMapper.findByUserId(userId);
        Integer studentId = student.getStudentId();

        if(experienceMapper.findStuByProjectExperienceId(projectExperienceId) == null){
            throw new RuntimeException("项目经历id不存在!");
        }

        if(!studentId.equals(experienceMapper.findStuByProjectExperienceId(projectExperienceId))){
            throw new RuntimeException("项目经历id在该学生中不存在!");
        }

        experienceMapper.deleteStuProject(projectExperienceId);
        experienceMapper.deleteProjectExperience(projectExperienceId);
    }

}
